<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">授权</button>
    </div>

    <div id="test12" class="demo-tree-more"></div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','tree','util'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree=layui.tree,
            util = layui.util,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {

                // 关闭弹出层
                layer.close(index);

                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

            });

            return false;
        });


        /**
         * 获取到roleid
         */
        var roleid = window.localStorage.getItem("roleid");
        $.ajax({
            url:'/tbRole/findByRoleid?roleid='+roleid,
            dataType:'json',
            success:function (res){
                if (res.code==0){
                    tree.render({
                        elem: '#test12'
                        ,data: res.data
                        ,showCheckbox: true  //是否显示复选框
                        ,id: 'demoId1'
                        ,isJump: true //是否允许点击节点时弹出新窗口跳转
                        ,click: function(obj){
                            var data = obj.data;  //获取当前点击的节点数据
                            layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                        }
                    });
                }
            }
        })

        //   console.log(data);
        //基本演示
        //按钮事件
        util.event('lay-demo', {
            getChecked: function (othis) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

                //1.checkedData 用户前端选中的权限
                //2.传输参数:1.perlist 权限列表。2.角色的id
                //3.在中间表中删除掉 该角色的所有的权限的关联关系

                console.log(checkedData);

                $.ajax({
                    url:'/tbRole/updateRole',
                    data:JSON.stringify({perlist:checkedData,roleid:roleid}),
                    contentType:"application/json",
                    type:"post",
                    dataType: 'json',
                    success:function (res){
                        if (res.code==0){
                            layer.msg(res.msg);
                        }
                    }
                })
            }
        });
    });




</script>
</body>
</html>